//
//  InstantAddDetailView.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/24.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

class InstantAddDetailView: UIView, UITextFieldDelegate {
    
    let typeViewHeight: CGFloat = 28.0
    let typeBtnHeight: CGFloat = 34.0
    
    var newEvent = Event()
    
    var date: NSDate! = nil
    var isWholeDay: Bool = false

    @IBOutlet weak var titleTextField: UITextField!
    @IBOutlet weak var descriptionTextField: UITextField!
    
    @IBOutlet weak var typeView0: UIView!
    @IBOutlet weak var typeView1: UIView!
    @IBOutlet weak var typeView2: UIView!
    @IBOutlet weak var typeView3: UIView!
    @IBOutlet weak var typeView4: UIView!
    
    @IBOutlet weak var typeBtn4: UIButton!
    @IBOutlet weak var typeBtn3: UIButton!
    @IBOutlet weak var typeBtn2: UIButton!
    @IBOutlet weak var typeBtn1: UIButton!
    @IBOutlet weak var typeBtn0: UIButton!
    
    @IBOutlet weak var timeLabel: UILabel!
    
    @IBOutlet weak var finishedBtn: UIButton!
    
    var typeBtns = [UIButton]()
    var selectedTypeBtn: UIButton! {
        didSet {
            selectedTypeBtn.backgroundColor = YOKOColor.TypeColors[selectedTypeBtn.tag]
        }
    }
    
    @IBOutlet weak var detailView: UIView!
    
    override func awakeFromNib() {
        
        typeBtn0.layer.cornerRadius = typeBtnHeight/2
        typeBtn0.tag = 0
        typeBtn1.layer.cornerRadius = typeBtnHeight/2
        typeBtn1.tag = 1
        typeBtn2.layer.cornerRadius = typeBtnHeight/2
        typeBtn2.tag = 2
        typeBtn3.layer.cornerRadius = typeBtnHeight/2
        typeBtn3.tag = 3
        typeBtn4.layer.cornerRadius = typeBtnHeight/2
        typeBtn4.tag = 4
        
        typeBtns = [typeBtn0, typeBtn1, typeBtn2, typeBtn3, typeBtn4]
        
        typeView0.layer.cornerRadius = typeViewHeight/2
        typeView1.layer.cornerRadius = typeViewHeight/2
        typeView2.layer.cornerRadius = typeViewHeight/2
        typeView3.layer.cornerRadius = typeViewHeight/2
        typeView4.layer.cornerRadius = typeViewHeight/2
        
        detailView.layer.cornerRadius = 2.5
        timeLabel.layer.cornerRadius = 5.0
        timeLabel.layer.masksToBounds = true

        
        cancelAddView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(self.cancelAdd)))
        titleTextField.becomeFirstResponder()
        titleTextField.returnKeyType = .Next
        titleTextField.delegate = self
        
        selectedTypeBtn = typeBtn0
        
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        descriptionTextField.becomeFirstResponder()
        return true
    }
    
    @IBAction func addEventWithFinishedBtn(sender: UIButton) {
        //TODO 给newEvent 赋值
        
        if titleTextField.text == "" {
            self.removeFromSuperview()
            return
        }
        
        newEvent.timeBegin = self.date!
        newEvent.timeEnd = self.date!
        newEvent.wholeDayEvent = self.isWholeDay
        newEvent.type = selectedTypeBtn.tag
        newEvent.summary = titleTextField.text!
        newEvent.descriptions = descriptionTextField.text!
        newEvent.localId = Int(NSDate().timeIntervalSince1970 * 1000)
        newEvent.calendarId = NSUserDefaults.getCurrentCalendarId()
        
        try! uiRealm.write({
            uiRealm.add(newEvent)
            NSLog("newEvent localId = %i", newEvent.localId)
        })
        
        if newEvent.wholeDayEvent{
            let fireDates = [newEvent.timeBegin.add(0, months: 0, weeks: 0, days: 0, hours: 8, minutes: 0, seconds: 0, nanoseconds: 0)]
            AlarmController.notificationFirstSet(fireDates, event: newEvent)
        }
        
        let api = ApiSync()
        api.updateEventToService(newEvent, isAdd: true, calendarId: NSUserDefaults.getCurrentCalendarId(), callback: {
            (response, error) in
            if (response != nil && response!["result"] as! Bool == true) {
//                print(response)
                try! uiRealm.write({
                    self.newEvent.uuid = response!["data"]!["uuid"] as! String
                    self.newEvent.calendarId = NSUserDefaults.getCurrentCalendarId()
                    self.newEvent.etag = response!["data"]!["etag"] as! String
                    self.newEvent.version = response!["data"]!["version"] as! Int
                })
            }
        })
        
        self.removeFromSuperview()
    }
    
    @IBOutlet weak var cancelAddView: UIView!
    
    @IBAction func cancelAdd() {
        self.removeFromSuperview()
    }
    
    @IBAction func typeBtnSelected(sender: UIButton) {
        
        selectedTypeBtn.backgroundColor = UIColor.clearColor()
        selectedTypeBtn = sender
        
    }

}
